﻿namespace SecutityLib
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class BinaryConverter
    {
        /// <summary>
        /// Toes the binary.
        /// </summary>
        /// <param name="numeral">The numeral.</param>
        /// <returns>binary value</returns>
        public static BitArray ToBinary(int numeral)
        {
            BitArray binary = new BitArray(new int[] { numeral });
            bool[] bits = new bool[binary.Count];
            binary.CopyTo(bits, 0);
            return binary;
        }

        /// <summary>
        /// Toes the numeral.
        /// </summary>
        /// <param name="binary">The binary.</param>
        /// <param name="length">The length.</param>
        /// <returns>numerical value</returns>
        public static int ToNumeral(BitArray binary, int length)
        {
            int numeral = 0;
            for (int i = 0; i < length; i++)
            {
                if (binary[i])
                {
                    numeral = numeral | (((int)1) << (length - 1 - i));
                }
            }

            return numeral;
        }
    }
}
